# -*- coding: UTF-8 -*-
# server_assets
# @File : randomObject.py
# @Date : 2023-09-01
# @Author  : wuzhouhai
# @Desc:
import KBEngine
from KBEDebug import *

import random

'''
@seq:对象数组
@probabilities:随机概率数组 升序排序

'''


def random_pick(seq, probabilities):
    """按照权值挑选一个"""
    #     print 'seq: %s'%seq
    #     print 'probabilities: %s'%probabilities

    randomProb = 0

    for num in probabilities:
        randomProb += num

    if not randomProb:
        return 0
    x = random.randint(1, randomProb)
    cumulative_probability = 0

    #     print u'随机值:%d'%x
    for item, item_probability in zip(seq, probabilities):
        if item_probability == 0:
            continue
        cumulative_probability += item_probability
        if x <= cumulative_probability:
            return item

    return 0


def random_picks(seq, probabilities, num, isRepeat=0):
    """按照权值挑选多个
    @return:list
    """
    selectList = []
    for _ in range(num):
        if not seq or not probabilities:
            break
        selectItem = random_pick(seq, probabilities)
        if selectItem != 0:
            selectList.append(selectItem)
            if not isRepeat:
                index = seq.index(selectItem)
                del seq[index]
                del probabilities[index]

    return selectList